package com.zhang.demo.base

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.zhang.demo.MyApplication
import com.zhang.demo.injector.component.ActivityComponent
import com.zhang.demo.injector.component.ApplicationComponent
import com.zhang.demo.injector.component.DaggerActivityComponent
import com.zhang.demo.injector.module.ActivityModule

/**
 * Created by zhangke on 2020/8/21 22:57
 * E-Mail Address：2426017569@qq.com
 */
open class BaseActivity : AppCompatActivity() {

    protected var mActivityComponent: ActivityComponent? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        getApplicationComponent()?.inject(this)
    }
    fun activityComponent(): ActivityComponent? {
        if (null == mActivityComponent) {
            mActivityComponent = DaggerActivityComponent.builder()
                .activityModule(getActivityModule())
                .applicationComponent(getApplicationComponent())
                .build()
        }
        return mActivityComponent
    }
    protected fun getApplicationComponent(): ApplicationComponent? {
        return (getApplication() as MyApplication).getApplicationComponent()
    }
    protected fun getActivityModule(): ActivityModule {
        return ActivityModule(this)
    }
}